﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Phoenix.AppCode.Data
{
    public static class Util
    {
        public static T Parse<T>(object value)
        {
            try { return (T)System.ComponentModel.TypeDescriptor.GetConverter(typeof(T)).ConvertFrom(value.ToString()); }
            catch (Exception) { return default(T); }
        }

        public static string ReplaceChar(string str)
        {
            return (str.Replace(' ', '-').Replace('.', '-').Replace('/', '-').Replace('+', '-').Replace('*', '-').Replace('?', '-').Replace('!', '-').Replace('@', '-').Replace('~', '-').Replace('#', '-').Replace('$', '-').Replace('%', '-').Replace('^', '-').Replace('(', '-').Replace(')', '-').Replace('=', '-').Replace('|', '-').Replace('{', '-').Replace('}', '-').Replace(':', '-').Replace('"', '-').Replace(';', '-').Replace('<', '-').Replace('>', '-').Replace("'", "-").Replace("]", "-").Replace("[", "-").Replace("\\", "-").Replace(",", "-").Replace("“", "").Replace("”", "").Replace("_", "-").Replace("---", "-").Replace("--", "-")).ToLower();
        }

        private static readonly string[] VietNamChar = new string[] 
        { 
            "aAeEoOuUiIdDyY", 
            "áàạảãâấầậẩẫăắằặẳẵ", 
            "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ", 
            "éèẹẻẽêếềệểễ", 
            "ÉÈẸẺẼÊẾỀỆỂỄ", 
            "óòọỏõôốồộổỗơớờợởỡ", 
            "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ", 
            "úùụủũưứừựửữ", 
            "ÚÙỤỦŨƯỨỪỰỬỮ", 
            "íìịỉĩ", 
            "ÍÌỊỈĨ", 
            "đ", 
            "Đ", 
            "ýỳỵỷỹ", 
            "ÝỲỴỶỸ"
        };

        public static string Filter(string str)
        {
            //Thay thế và lọc dấu từng char      
            for (int i = 1; i < VietNamChar.Length; i++)
            {
                for (int j = 0; j < VietNamChar[i].Length; j++)
                    str = str.Replace(VietNamChar[i][j], VietNamChar[0][i - 1]);
            }
            return str;
        }

        public static String CutString(String str, int length)
        {
            Boolean flag = true;
            while (flag)
            {
                try
                {
                    string temp = str.Substring(str.IndexOf('<'), str.IndexOf('>') - str.IndexOf('<') + 1);
                    str = str.Replace(temp, "");
                    if (temp == "")
                        flag = false;
                }
                catch (Exception)
                {
                    flag = false;
                }
            }
            str = str.Length > length ? str.Substring(0, length).Substring(0, str.Substring(0, length).LastIndexOf(' ')) + " ..." : str;
            return str.Replace("  ", " ");
        }

        public static string RemoveHtmlTags(this string htmlString)
        {
            var array = new char[htmlString.Length];
            var arrayIndex = 0;
            var inside = false;

            foreach (var @let in htmlString)
            {
                if (let == '<')
                {
                    inside = true;
                    continue;
                }
                if (let == '>')
                {
                    inside = false;
                    continue;
                }
                if (inside) continue;
                array[arrayIndex] = let;
                arrayIndex++;
            }
            return new string(array, 0, arrayIndex);
        }
    }
}